﻿using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RTW.OA.Areas.System.Servers
{
    public class SysMenuServers
    {
        private readonly IRepository<sys_menu> Sys_MenuRepository;
        private readonly IRepository<actionpermission> PermissionRepository;
        public SysMenuServers()
        {
            Sys_MenuRepository = new DapperRepository<sys_menu>();
            PermissionRepository=new DapperRepository<actionpermission>();
        }

        public List<sys_menu> GetSys_Menus()
        {
            return Sys_MenuRepository.GetAllEnumerable().ToList();
        }

        public sys_menu GetSys_MenusById(string id)
        {
            return Sys_MenuRepository.GetById(id);
        }


        public List<actionpermission> GetPermissions(string id)
        {
            if(!string.IsNullOrEmpty(id))
            {
                string sql = "select * from actionpermission where MENU_ID='" + id + "'";
                return PermissionRepository.SqlWithParams(sql).ToList();
            }
            return null;
        }

        public void AddAction(actionpermission model)
        {
            PermissionRepository.Insert(model);
        }

        internal void DelAction(int id)
        {
            PermissionRepository.DeleteByIds(id.ToString());
        }

        internal void AddMenu(sys_menu model)
        {
            Sys_MenuRepository.Insert(model);
        }

        internal void UpdateMenu(sys_menu model)
        {
            Sys_MenuRepository.Update(model);
        }

        internal void Del(string  id)
        {
            Sys_MenuRepository.DeleteByIds(id);
        }
    }
}